package DEMO;
//使用不同的锁对象，对同一个静态变量进行自增
public class demo_4 {
    public static void main(String[] args) throws InterruptedException {
        Counter4 counter=new Counter4();
        Counter4 counter2=new Counter4();
        Thread  thread1=new Thread(()->{
            for (int i = 0; i <5_0000 ; i++) {
                counter.increase();
            }
        });
        Thread thread2 =new Thread(()->{
            for (int i = 0; i <5_0000 ; i++) {
                counter2.increase();
            }
        });
        //启动线程
        thread1.start();
        thread2.start();
        //等待线程执行完成
        thread1.join();
        thread2.join();
        System.out.println("count= "+counter2.count);
    }
}
class Counter4{
    public static int count=0;
    //一个方法加锁了，一个方法没有加锁
    public  synchronized  void increase(){
        count++;
    }

}
